package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.pojo.ShoppingCart;
import com.reggie.service.ShoppingCartService;
import com.reggie.utils.BaseContext;
import com.reggie.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhaoyuheng
 * @email 2646498606@qq.com
 * @date 2022/4/27 18:00
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 获取当前用户购物车信息
     * @return
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        //获取当前用户id
        Long userId = BaseContext.getCurrent();
        //获取当前用户的购物车信息
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);
        return Result.success(shoppingCartList);
    }

    /**
     * 购物车添加商品
     * todo 问题：如果两次添加口味不同，应该怎么办
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //获取当前用户id，给购物车属性赋值
        Long userId = BaseContext.getCurrent();
        shoppingCart.setUserId(userId);

        //查询当前菜品或者套餐是否已经存在
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //条件：根据用户Id查询
        wrapper.eq(ShoppingCart::getUserId, userId);


        //根据传回字段添加查询条件
        if (dishId != null) {
            //添加的是菜品，查询是否存在该菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加的是套餐,查询是否存在改套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }


        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        //已经存在，在原来的数量基础上加1
        if (cart != null) {
            cart.setNumber(cart.getNumber() + 1);
            //更新数据
            shoppingCartService.updateById(cart);
        } else {
            //不存在，添加到购物车，默认数量是1
            shoppingCart.setNumber(1);
            //添加创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartService.save(shoppingCart);
            cart = shoppingCart;
        }


        return Result.success(cart);
    }

    /**
     * 减少购物车商品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info(shoppingCart.toString());
        //获取当前登录用户Id
        Long userID = BaseContext.getCurrent();
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //条件：根据登录用户查购物车信息
        wrapper.eq(ShoppingCart::getUserId, userID);

        //根据传入的数据不同添加不同的参数
        if (shoppingCart.getDishId() != null) {
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //查询数据，且判断数据的数量是否为1
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        if (cart == null) {
            return Result.error("当前菜品已清除");
        }
        if (cart.getNumber() == 1) {
            //只剩下一个，直接删除
            shoppingCartService.removeById(cart);
        } else {
            //将数量减一
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartService.updateById(cart);
        }

        return Result.success(cart);
    }

    /**
     * 清理空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> clean() {
        //获取登陆人Id
        Long userId = BaseContext.getCurrent();
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null, ShoppingCart::getUserId, userId);

        shoppingCartService.remove(wrapper);
        return Result.success("清除成功");
    }


}
